﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QLTV.Forms.BaoMat
{
    public partial class frmUserList : BaseForm
    {
        public frmUserList()
        {
            InitializeComponent();
        }

        private void frmUserList_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsUsers.tblNhanSu' table. You can move, or remove it, as needed.
            this.tblNhanSuTableAdapter.Fill(this.dsUsers.tblNhanSu);
            // TODO: This line of code loads data into the 'dsUsers.tblPhong' table. You can move, or remove it, as needed.
            this.tblPhongTableAdapter.Fill(this.dsUsers.tblPhong);
            // TODO: This line of code loads data into the 'dsUsers.tblRole' table. You can move, or remove it, as needed.
            this.tblRoleTableAdapter.Fill(this.dsUsers.tblRole);
        }


        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                frmNhanSu f = new frmNhanSu();
                f.StartPosition = FormStartPosition.CenterParent;
                f.manhansu = gridView1.GetFocusedRowCellValue("iNhanSuID").ToString();
                f.isNew = false;
                f.ShowDialog();
                tblNhanSuBindingSource.DataSource = this.tblNhanSuTableAdapter.GetData();
            }
            else
                XtraMessageBox.Show("Xin vui lòng chọn bản ghi", "Thông báo");

        }

        private void tblUsersBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            //  MessageBox.Show(((DataRowView)tblUsersBindingSource.Current).Row.RowState.ToString());
        }

        private void tblUsersBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
          
        }
        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
                this.tblUsersTableAdapter.FillByMaNhanSu(this.dsUsers.tblUsers, gridView1.GetFocusedRowCellValue("iNhanSuID").ToString());
        }

        private void cmdThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmNhanSu f = new frmNhanSu();
            f.StartPosition = FormStartPosition.CenterParent;
            f.isNew = true;
            f.ShowDialog();
            tblNhanSuBindingSource.DataSource = this.tblNhanSuTableAdapter.GetData();
            
        }
        private void cmdSua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
        }
        private void cmdXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                if (XtraMessageBox.Show("Bạn chắc chắn muốn xóa?", "Cảnh báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    tblNhanSuBindingSource.RemoveCurrent();
                    tblNhanSuBindingSource.EndEdit();
                    tblNhanSuTableAdapter.Update(dsUsers.tblNhanSu);
                   
                    tblNhanSuBindingSource.DataSource = this.tblNhanSuTableAdapter.GetData();
                     
                }
            }
            else
                XtraMessageBox.Show("Xin vui lòng chọn bản ghi", "Thông báo");
        }

        private void huy_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
                if (XtraMessageBox.Show("Bạn chắc chắn muốn xóa?", "Cảnh báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    try
                    {
                        tblUsersBindingSource.RemoveCurrent();
                        tblUsersBindingSource.EndEdit();
                        tblUsersTableAdapter.Update(dsUsers.tblUsers);
                        tblUsersBindingSource.DataSource = this.tblUsersTableAdapter.GetDataByMaNhanSu(gridView1.GetFocusedRowCellValue("iNhanSuID").ToString());
                        alertControl1.Show(this, "Thông báo", "Cập nhật thành công");
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show(ex.ToString(), "Cảnh báo");
                    }
                }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (gridView1.FocusedRowHandle >= 0)
            {
                frmNhanSu f = new frmNhanSu();
                f.StartPosition = FormStartPosition.CenterParent;
                f.manhansu = gridView1.GetFocusedRowCellValue("iNhanSuID").ToString();
                f.isNew = false;
                f.ShowDialog();
                tblNhanSuBindingSource.DataSource = this.tblNhanSuTableAdapter.GetData();
            }
            else
                XtraMessageBox.Show("Xin vui lòng chọn bản ghi", "Thông báo");
        }
    }
}